﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using SegIdenPC.Entidad;
using SegIdenPC.Herramientas;

namespace SegIdenPC.Utilitarios
{
    /// <summary>
    /// Clase       : ImprimirDocumento
    /// Descripcion : Contiene la logica del formato de impresion
    /// Autor       : 
    /// Fecha       : 04/05/2012
    /// Empresa     : CasaDesign
    /// </summary>
    public class ImprimirDocumentoVenta
    {
        #region --- Variables y Constantes ---
        // Formato de impresion
        private const string FORMATO_MONTO = "#0.00";
        private const string FORMATO_SIMBOLO_MONTO = "{0} {1}";
        private const string FORMATO_FECHA_DOCUMENTO = "dd/MM/yyyy";

        private Font fuentePrincipal = new Font("Arial", 10);
        private Font fuenteTotal = new Font("Arial", 8);
        private Venta ventaPrincipal = null;
        private Facturacion facturaPrincipal = null;
        private PrintDocument printDocument = null;
        #endregion

        #region --- Constructor ---
        public ImprimirDocumentoVenta(Venta venta, Facturacion factura)
        {
            this.ventaPrincipal = venta;
            this.facturaPrincipal = factura;
            this.printDocument = new PrintDocument();
        }
        #endregion

        #region --- Eventos ---
        private void printDocument_PrintBoleta_004(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 2;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;

            // Calculando el numero de lineas por pagina
            linesPerPage = e.MarginBounds.Height / this.fuentePrincipal.GetHeight(e.Graphics);
            linesPerPage -= 3;

            count++;
            yPos = topMargin + ((float)((count-1)*1.3) * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo razon social del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics)) + 10;

            // Imprimiendo ruc del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.nroDocumento,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics)) + 17;

            // Imprimiendo direccion del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.direccion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());


            // Imprimiendo fecha en que se realizó la venta
            e.Graphics.DrawString(Convert.ToDateTime(this.facturaPrincipal.fechaRegistro).ToString(FORMATO_FECHA_DOCUMENTO),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 600,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            //ListaLineaVenta listaLineaDocumento = this.ventaPrincipal.listaLineaVenta;
            ListaLineaFacturacion listaLineaDocumento = this.facturaPrincipal.listaLineaFacturacion;
            int numero = 0;
            int countTotales = 0;

            count += 4;
            countTotales = count;

            // Imprimir detalle del documento
            while (count < linesPerPage && (numero < listaLineaDocumento.size()))
            {
                if (numero == 0) yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 1) yPos = topMargin + ((float)(count - 0.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 2) yPos = topMargin + ((float)(count - 0.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 3) yPos = topMargin + ((float)(count - 1.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 4) yPos = topMargin + ((float)(count - 1.4) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 5) yPos = topMargin + ((float)(count - 2.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 6) yPos = topMargin + ((float)(count - 2.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 7) yPos = topMargin + ((float)(count - 2.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 8) yPos = topMargin + ((float)(count - 3.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 9) yPos = topMargin + ((float)(count - 3.5) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 10) yPos = topMargin + ((float)(count - 3.8) * this.fuentePrincipal.GetHeight(e.Graphics));

                // Escribir linea de caracteres cantidad
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].cantidad.ToString(),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres descripcion del producto
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].descripcion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 40,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres precio 
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           listaLineaDocumento.lista[numero].precio.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 605,
                                      yPos,
                                      new StringFormat());
                count += 2;
                numero++;
            }
            // Imprimir totales de acuerdo al tipo de documento
            countTotales += 20;

            yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo Total
            e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                       this.facturaPrincipal.totalFacturacion.ToString(FORMATO_MONTO)),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 605,
                                  yPos,
                                  new StringFormat());
        }
         
        private void printDocument_PrintFactura_004(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 1;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;

            // Calculando el numero de lineas por pagina
            linesPerPage = e.MarginBounds.Height / this.fuentePrincipal.GetHeight(e.Graphics);
            linesPerPage -= 3;

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo razon social del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics)) + 10;

            // Imprimiendo ruc del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.ruc,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + ((float)((count - 1) * 1.3) * this.fuentePrincipal.GetHeight(e.Graphics)) + 17;

            // Imprimiendo direccion del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.direccion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());


            // Imprimiendo fecha actual
            e.Graphics.DrawString(Convert.ToDateTime(this.facturaPrincipal.fechaRegistro).ToString(FORMATO_FECHA_DOCUMENTO),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 600,
                                  yPos,
                                  new StringFormat());

            count+=4;
            yPos = topMargin + ((float)((count - 2) * 1.3) * this.fuentePrincipal.GetHeight(e.Graphics));

            //Imprimiendo la palabra "P. UNIT."
            e.Graphics.DrawString("P. UNIT.",
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 530,
                                  yPos,
                                  new StringFormat());

            ListaLineaFacturacion listaLineaDocumento = this.facturaPrincipal.listaLineaFacturacion;
            int numero = 0;
            int countTotales = 0;

            count ++;
            countTotales = count;

            // Imprimir detalle del documento
            while (count < linesPerPage && (numero < listaLineaDocumento.size()))
            {
                if (numero ==0) yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 1 || numero ==2) yPos = topMargin + ((float)(count - 0.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 3) yPos = topMargin + ((float)(count - 0.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 4) yPos = topMargin + ((float)(count - 1.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 5) yPos = topMargin + ((float)(count - 1.4) * this.fuentePrincipal.GetHeight(e.Graphics));

                // Escribir linea de caracteres cantidad
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].cantidad.ToString(),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres descripcion del producto
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].descripcion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 40,
                                      yPos,
                                      new StringFormat());

               //Escribir el Precio Unitario
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           Math.Round((listaLineaDocumento.lista[numero].precio / Convert.ToInt32(listaLineaDocumento.lista[numero].cantidad)),2).ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 530,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres precio 
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           listaLineaDocumento.lista[numero].precio.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 605,
                                      yPos,
                                      new StringFormat());
                count+=2;
                numero++;
            }

            countTotales += 16;

            yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo el monto en palabras
            e.Graphics.DrawString("SON: " + Convertir_Numero(this.facturaPrincipal.totalFacturacion.ToString(FORMATO_MONTO)) + " " +
                                  this.facturaPrincipal.moneda.descripcion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

                countTotales += 3;

                yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));

                // Imprimiendo SubTotal
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           this.facturaPrincipal.subTotalFacturacion.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 355,
                                      yPos,
                                      new StringFormat());

                // Imprimiendo IGV
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           this.facturaPrincipal.igvFacturacion.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 483,
                                      yPos,
                                      new StringFormat());

                // Imprimiendo Total
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           this.facturaPrincipal.totalFacturacion.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 605,
                                      yPos,
                                      new StringFormat());
       
        }

        private void printDocument_PrintBoleta_0001(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 2;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;

            // Calculando el numero de lineas por pagina
            linesPerPage = e.MarginBounds.Height / this.fuentePrincipal.GetHeight(e.Graphics);
            linesPerPage -= 3;

            count++;
            yPos = topMargin + ((float)(count-1)  * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo razon social del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo ruc del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.nroDocumento,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());

            count+=2;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo direccion del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.direccion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 40,
                                  yPos,
                                  new StringFormat());


            // Imprimiendo fecha en que se realizó la venta
            e.Graphics.DrawString(Convert.ToDateTime(this.facturaPrincipal.fechaRegistro).ToString(FORMATO_FECHA_DOCUMENTO),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 600,
                                  yPos,
                                  new StringFormat());

            //count++;
            //yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            ListaLineaFacturacion listaLineaDocumento = this.facturaPrincipal.listaLineaFacturacion;
            int numero = 0;
            int countTotales = 0;

            count += 4;
            countTotales = count;

            // Imprimir detalle del documento
            while (count < linesPerPage && (numero < listaLineaDocumento.size()))
            {
                if (numero == 0) yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 1) yPos = topMargin + ((float)(count - 0.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 2) yPos = topMargin + ((float)(count - 0.8) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 3) yPos = topMargin + ((float)(count - 1.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 4) yPos = topMargin + ((float)(count - 1.7) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 5) yPos = topMargin + ((float)(count - 2.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 6) yPos = topMargin + ((float)(count - 2.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 7) yPos = topMargin + ((float)(count - 3.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 8) yPos = topMargin + ((float)(count - 3.5) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 9) yPos = topMargin + ((float)(count - 3.8) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 10) yPos = topMargin + ((float)(count - 4) * this.fuentePrincipal.GetHeight(e.Graphics));

                // Escribir linea de caracteres cantidad
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].cantidad.ToString(),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin - 20,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres descripcion del producto
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].descripcion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 40,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres precio 
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           listaLineaDocumento.lista[numero].precio.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 630,
                                      yPos,
                                      new StringFormat());
                count += 2;
                numero++;
            }
            // Imprimir totales de acuerdo al tipo de documento
            countTotales += 19;

            yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo Total
            e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                       this.facturaPrincipal.totalFacturacion.ToString(FORMATO_MONTO)),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 630,
                                  yPos,
                                  new StringFormat());
        }

        private void printDocument_PrintFactura_0001(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0; //
            int count = 1;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;

            // Calculando el numero de lineas por pagina
            linesPerPage = e.MarginBounds.Height / this.fuentePrincipal.GetHeight(e.Graphics);
            linesPerPage -= 3;

            count++;
            yPos = topMargin + ((float)(count-6.5) * this.fuentePrincipal.GetHeight(e.Graphics));//-0.5

            // Imprimiendo razon social del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + ((float)(count - 5.5) * this.fuentePrincipal.GetHeight(e.Graphics)); // 10

            // Imprimiendo ruc del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.ruc,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin,
                                  yPos,
                                  new StringFormat());

            count++;
            //yPos = topMargin + ((float)((count - 1) * 1.1) * this.fuentePrincipal.GetHeight(e.Graphics)) + 11;//
            yPos = topMargin + ((float)((count - 4.5)) * this.fuentePrincipal.GetHeight(e.Graphics));//
                        
            // Imprimiendo fecha en que se realizó la venta
            e.Graphics.DrawString(Convert.ToDateTime(this.facturaPrincipal.fechaRegistro).ToString(FORMATO_FECHA_DOCUMENTO),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 543,
                                  yPos,
                                  new StringFormat());

            count++;
            //yPos = topMargin + ((float)((count - 4)) * this.fuentePrincipal.GetHeight(e.Graphics)); //17

            // Imprimiendo direccion del cliente
            e.Graphics.DrawString(this.ventaPrincipal.cliente.direccion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin,
                                  yPos,
                                  new StringFormat());

            //Se incremento de 4 a 5 por que el formato real imprime mal
            count += 1;//5 //2
            
            ListaLineaFacturacion listaLineaDocumento = this.facturaPrincipal.listaLineaFacturacion;
            int numero = 0;
            int countTotales = 0;
            countTotales = count;

            // Imprimir detalle del documento
            while (count < linesPerPage && (numero < listaLineaDocumento.size()))
            {
                if (numero == 0) yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 1 || numero == 2) yPos = topMargin + ((float)(count - 0.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 3) yPos = topMargin + ((float)(count - 0.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 4) yPos = topMargin + ((float)(count - 1.1) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 5) yPos = topMargin + ((float)(count - 1.4) * this.fuentePrincipal.GetHeight(e.Graphics));

                // Escribir linea de caracteres cantidad
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].cantidad.ToString(),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin -75,//-30
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres descripcion del producto
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].descripcion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin, //40
                                      yPos,
                                      new StringFormat());

                //Escribir el Precio Unitario
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           Math.Round((listaLineaDocumento.lista[numero].precio / Convert.ToInt32(listaLineaDocumento.lista[numero].cantidad)), 2).ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 600, //535 580
                                      yPos,
                                      new StringFormat());

              /* // Escribir linea de caracteres precio 
                e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                           listaLineaDocumento.lista[numero].precio.ToString(FORMATO_MONTO)),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 565,//605
                                      yPos,
                                      new StringFormat());*/
                count += 2;
                numero++;
            }

            countTotales += 33; //15

            yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo el monto en palabras
            e.Graphics.DrawString(Convertir_Numero(this.facturaPrincipal.totalFacturacion.ToString(FORMATO_MONTO)) + " " +
                                  this.facturaPrincipal.moneda.descripcion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin,
                                  yPos,
                                  new StringFormat());

            countTotales += 3;//

            yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics))+4;//1

            // Imprimiendo SubTotal
            e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                       this.facturaPrincipal.subTotalFacturacion.ToString(FORMATO_MONTO)),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 335, //605 //355
                                  yPos,
                                  new StringFormat());
            //countTotales ++;

            //yPos = topMargin + ((float)(countTotales+0.6) * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo IGV
            e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                       this.facturaPrincipal.igvFacturacion.ToString(FORMATO_MONTO)),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 472,//605 485
                                  yPos,
                                  new StringFormat());
            //countTotales += 2;

           // yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));
            // Imprimiendo Total
            e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.facturaPrincipal.moneda.simbolo,
                                                                       this.facturaPrincipal.totalFacturacion.ToString(FORMATO_MONTO)),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 600,//605
                                  yPos,
                                  new StringFormat());

        }

        #endregion

        #region --- Metodos Propios ---
        public void establecerImpresora(string direccionImpresora)
        {
            this.printDocument.PrinterSettings.PrinterName = direccionImpresora;
        }

        public void ejecutarFactura004()
        {
            this.printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintFactura_004);
            this.printDocument.Print();
        }

        public void ejecutarFactura0001()
        {
            this.printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintFactura_0001);
            this.printDocument.Print();
        }

        public void ejecutarBoleta004()
        {
            this.printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintBoleta_004);
            this.printDocument.Print();
        }

        public void ejecutarBoleta0001()
        {
            this.printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintBoleta_0001);
            this.printDocument.Print();
        }


        string[] NumeroBase ={
                                 "",
                                 "Uno",
                                 "Dos",
                                 "Tres",
                                 "Cuatro",
                                 "Cinco",
                                 "Seis",
                                 "Siete",
                                 "Ocho",
                                 "Nueve",
                                 "Diez",
                                 "Once",
                                 "Doce",
                                 "Trece",
                                 "Catorce",
                                 "Quince",
                                 "Dieciseis",
                                 "Diecisiete",
                                 "Dieciocho",
                                 "Diecinueve",
                            };
        string[] NumeroBase2 = {
                                   "",
                                   "",
                                   "Veinte",
                                   "Treinta",
                                   "Cuarenta",
                                   "Ciencuenta",
                                   "Sesenta",
                                   "Setenta",
                                   "Ochenta",
                                   "Noventa"
                               };
        
        public string Convertir_Numero(string sNumeroConvertir)
        {
            string Resultado = "";
            string num = sNumeroConvertir.Substring(0, sNumeroConvertir.Length - 3);
            string decimales = sNumeroConvertir.Substring(sNumeroConvertir.Length - 2, 2);
           
            int Tama_Cadena = num.Length;
            
            if (Tama_Cadena < 4)//3
                Resultado = Trio(Tama_Cadena, num);
            else if (Tama_Cadena < 7)//6
            {
                int millares = Tama_Cadena - 3;
                if (num.Substring(0, 1) == "1" && Tama_Cadena == 4)
                    Resultado = "Mil " + Trio(3, num.Substring(millares, 3)) + " y " +decimales +"/100";
                else
                    Resultado = Trio(millares, num.Substring(0, millares)) + " mil " + Trio(3, num.Substring(millares, 3)) + " y " + decimales + "/100";
            }
            else if (Tama_Cadena < 10)//9
                Resultado = "";
            else if (Tama_Cadena < 13)
                Resultado = "";
            else if (Tama_Cadena < 16)
                Resultado = "";
            else if (Tama_Cadena < 19)
                Resultado = "";
            else if (Tama_Cadena < 21)
                Resultado = "";
            else if (Tama_Cadena < 24)
                Resultado = "";
            return Resultado;
        }
        
        string Unidades(string numx)
        {
            return NumeroBase[Convert.ToInt32(numx)];
        }
        
        string Decenas(string numx)
        {
            string Pre = "";
            int Num = Convert.ToInt32(numx);
            if (Num < 20)
            {
                Pre = NumeroBase[Num];
            }
            else
            {
                if (numx.Substring(0, 1) == "2")
                    Pre = NumeroBase2[2] + Unidades(numx.Substring(1, 1));
                else
                {
                    if (numx.Substring(1, 1) == "0")
                        Pre = NumeroBase2[Convert.ToInt32(numx.Substring(0, 1))];
                    else
                        Pre = NumeroBase2[Convert.ToInt32(numx.Substring(0, 1))] + " y " + Unidades(numx.Substring(1, 1));
                }
            }
            return Pre;
        }
        
        string Centenas(string numx)
        {
            string Pre = "";
            if (numx.Substring(0, 1) == "1")
            {
                if (numx.Substring(1, 1) == "0" && numx.Substring(2, 1) == "0")
                    Pre = "Cien ";
                else
                    Pre = "ciento " + Decenas(numx.Substring(1, 2));
            }
            else if (numx.Substring(0, 1) == "0")
            {
                Pre = "" + Decenas(numx.Substring(1, 2));
            }
            else if (numx.Substring(0, 1) == "5")
            {
                Pre = "Quinientos " + Decenas(numx.Substring(1, 2));
            }
            else if (numx.Substring(0, 1) == "7")
            {
                Pre = "Setecientos " + Decenas(numx.Substring(1, 2));
            }
            else if (numx.Substring(0, 1) == "9")
            {
                Pre = "Novecientos " + Decenas(numx.Substring(1, 2));
            }
            else
            {
                Pre = NumeroBase[Convert.ToInt32(numx.Substring(0, 1))] + "cientos " + Decenas(numx.Substring(1, 2));
            }
            return Pre;
        }
        
        string Trio(int cant, string Val)
        {
            string CadenaFinal = "";
            switch (cant)
            {
                case 1: CadenaFinal = Unidades(Val);
                    break;
                case 2: CadenaFinal = Decenas(Val);
                    break;
                case 3: CadenaFinal = Centenas(Val);
                    break;
            }
            return CadenaFinal;
        }
        #endregion
    }
}
